package com.example.haojiutao.controller.user;

import com.example.haojiutao.common.Result;
import com.example.haojiutao.model.domain.Goods;
import com.example.haojiutao.model.domain.User;
import com.example.haojiutao.service.GoodsService;
import com.example.haojiutao.utils.H5LoginUserUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Description: 商品的控制层
 * @Author: ma_lihong
 * @CreateTime: 2024/1/23 16:53
 */
@Slf4j
@RestController
public class GoodsController {


    @Autowired
    private GoodsService goodsService;
    /*
     * @Description: 添加闲置
     * @Author: ma_lihong
     * @CreateTime: 2024/1/23 19:38
     * @param: goods
     * @return: com.example.haojiutao.common.Result
     */
    @CrossOrigin(origins = {"http://localhost:5173"}, allowCredentials = "true")
    @PostMapping("/user/goods") //等于@RequestMapping(method =POST ,value ="/path" )
    public Result insertGoods(@RequestBody Goods goods){
        log.info("添加商品：{}",goods);
        return goodsService.insertGoods(goods);
    }
    @CrossOrigin(origins = {"http://localhost:5173"}, allowCredentials = "true")
    @PostMapping("/user/xianzhi") //等于@RequestMapping(method =POST ,value ="/path" )
    public Result addGoods(@RequestBody Goods goods){
        log.info("发布闲置商品：{}",goods);
        return goodsService.addGoods(goods);
    }

    /*
     * @Description:更新商品价格，图片，详情，品牌
     * @Author: ma_lihong
     * @CreateTime: 2024/1/23 19:39
     * @param: goods
     * @return: com.example.haojiutao.common.Result
     */

    @PutMapping("/user/goods/{goodsId}")
    public Result updateGoods(@RequestBody@PathVariable int goodsId){
        Goods goods = new Goods();
        log.info("更新商品信息:goods{}",goods);
        goods.setGoodsId(goodsId);
        return goodsService.updateGoods(goods);

    }

    /*
     * @Description: 下架商品、删除商品
     * @Author: ma_lihong
     * @CreateTime: 2024/1/24 20:47
     * @param: goodId
     * @return: com.example.haojiutao.common.Result
     */
    @DeleteMapping("/user/goods/delete/{goodsId}")
    public Result deleteGoods(@PathVariable int goodsId){

        return goodsService.deleteById(goodsId);

    }
    /*
     * @Description:根据商品名称搜索商品，显示商品列表
     * @Author: ma_lihong
     * @CreateTime: 2024/1/25 21:04
     * @return: com.example.haojiutao.common.Result
     */
    @GetMapping("/user/goods/list")
    public Result selectByName(Goods goods){
        log.info("根据商品名查找");
        List<Goods> good = goodsService.selectByName(goods);
        if(good!=null){
            for (Goods goods1:good){
                System.out.println("################"+goods1.toString());
            }
        }else
        {
            System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!");
        }
        PageInfo<Goods> pageInfo = new PageInfo<>(good);
        return Result.success("查询成功",pageInfo);

    }
    /*
     * @Description: 查看单个物品，根据id查询,查看商品详情
     * @Author: ma_lihong
     * @CreateTime: 2024/1/26 14:21
     * @param: goods
     * @return: com.example.haojiutao.common.Result
     */
    @GetMapping("/user/goods/{goodsId}")
    public Result selectById(@PathVariable int goodsId){
        log.info("查看单个商品");
        Goods goods = goodsService.selectById(goodsId);
        return Result.success("查询成功！",goods);
    }

    /*
     * @Description: 查看我卖出的商品
     * @Author: ma_lihong
     * @CreateTime: 2024/1/25 16:12
     * @return: com.example.haojiutao.common.Result
     **/
    @CrossOrigin(origins={"http://localhost:5173"}, allowCredentials = "true")
    @GetMapping("/user/buy")
    public Result getRecommendBuy(Goods goods){
        User user = H5LoginUserUtil.getLoginUser();
        goods.setUserId(user.getId());
       // Goods goods=new Goods();
       // goods.setPageSize(2); //显示6条；
        return Result.success("查询成功！", goodsService.getRecommendbuy(goods));
    }
    /*
     * @Description: 查看我发布的的商品
     * @Author: ma_lihong
     * @CreateTime: 2024/1/28 16:13
     * @param: null
     * @return: null
     **/
    @CrossOrigin(origins={"http://localhost:5173"}, allowCredentials = "true")
    @GetMapping("/user/fabu")
    public Result getRecommendFabu(Goods goods){
        // goods.setPageSize(2); //显示6条；TODO ???
        User user = H5LoginUserUtil.getLoginUser();
        goods.setUserId(user.getId());
        return Result.success("查询成功！", goodsService.getRecommendFabu(goods));
    }




@CrossOrigin(origins={"http://localhost:5173"}, allowCredentials = "true")
    @GetMapping("/user/goodsid")
    public Result getGoodsByUserId(){
        Goods goods=new Goods();
        goods.setPageSize(2); //显示6条；
        return Result.success("查询成功！",
                goodsService.getGoodsByUserId(goods));
    }
    @DeleteMapping("/user/delistgoods/delete/{goodsId}")
    public Result delistGoods(@PathVariable Integer goodsId){
        return goodsService.delistById(goodsId);

    }
    @PutMapping("/user/listgoods/{goodsId}")
    public Result listGoods(@PathVariable Integer goodsId){

        return goodsService.listGoods(goodsId);

    }

}